function varargout = analySRQuality0(Y, scale, netname, solver_mode, bLoadSRResults)
% [psnrs, ssims, ImgList] = sranaly.analySRQuality(Y, scale, netname, hDeploy, solver_mode)
% Input:
%   -- Y: Ground truth image tensor/list or image path
% Output:
%   -- psnrs/ssims(:, 1) save PSNR/SSIM values for Bicubic method
%   -- psnrs/ssims(:, 2) save PSNR/SSIM values for Advanced (eg., CNN) method
%   -- ImgList{i, :} save {y0, x, x, yhat}
%       y0: ground truth
%       x:  low resolution image
%       x: bicubic reconstruction result
%       yhat:  CNN reconstruction result

% scale = 3;
% netname = 'FSRCNN';
% netname = 'VDSR0';
% netname = 'DRRN';
% netname = 'VDSR';

if nargin < 4, hDeploy = []; end
if nargin < 5, solver_mode = []; end
if nargin < 6, bLoadSRResults = false; end

if isempty(hDeploy)
    hDeploy = srmodel.getDeployFunc(netname, scale);
end

if isnumeric(Y)
    way = 1;
    ImgNum = size(Y, 3);
elseif iscell(Y)
    way = 2;
    ImgNum = length(Y);
elseif ischar(Y)
    way = 3;
    [Y, ImgSetName] = srdata.loadImgSet(Y);
    [bFound, ~, SRMatFile] = myinput.searchDataByParam(srpath.getSRImgSetHome, 'st-*.mat', [ImgSetName '-*.mat'], netname, scale);
end

%%
shift = [0 0];
starting = 'Bicubic->Bicubic';
switch upper(netname)
    case 'BICUBIC'
    case {'FSRCNN', 'FSRCNN_BATCH', 'FSRCNN-BATCH'}
        starting = 'Bicubic->I';
        if scale == 3, shift = [0 1]; end
    case 'VDSR0' % VDSR0 uses MatConvNet
        % gpu_id = eval(solver_mode(end));
        % gpuDevice(gpu_id + 1);
    otherwise
        mycaffe.init(solver_mode);
end
starting = strsplit(starting, '->');

%%
if isempty(hDeploy), num_deploy = 1; else num_deploy = 2; end
psnrs = zeros(ImgNum, num_deploy);
ssims = zeros(ImgNum, num_deploy);

nout = nargout;
if nout == 3
    ImgList = cell(ImgNum, 2);
end

%%
tic
for i = 1 : ImgNum
    switch way
        case 1
            y0 = Y(:, :, i);
        case 2
            y0 = Y{i};
        case 3
            y0 = Y{i};
            if bLoadSRResults
                [~, file_name] = fileparts(ImgFiles{i});
                MatFileNmae = fullfile(SRImgSetPath, [file_name, '.mat']);
            end
    end
    
    y0 = srimg.modcrop(y0, scale);
    
    %% Interpolation
    yhat0 = srimg.im2lr(y0, scale, starting{1}, starting{1});
    psnrs(i, 1) = srimg.psnr(y0, yhat0, scale, [0 0]);
    ssims(i, 1) = srimg.ssim(y0, yhat0, scale, [0 0]);
    
    %% CNN
    if isempty(hDeploy)
        yhat = yhat0; yhat0 = [];
    elseif bLoadSRResults && exist(MatFileNmae, 'file')
        % yhat = imread(fullfile(SRImgSetPath, ImgFiles{i}));
        yhat = importdata(MatFileNmae);
    else
        if ~strcmp(starting{1}, starting{2})
            yhat0 = srimg.im2lr(y0, scale, starting{1}, starting{2});
        end
        yhat = hDeploy(yhat0);
        
        if bLoadSRResults
            % imwrite(yhat, fullfile(SRImgSetPath, ImgFiles{i}));
            save(MatFileNmae, 'yhat');
        end
    end
    
    %%
    if num_deploy == 2
        psnrs(i, 2) = srimg.psnr(y0, yhat, scale, shift);
        ssims(i, 2) = srimg.ssim(y0, yhat, scale, shift);
    end
    
    fprintf('\tFor Image %g/%g, PSNR/SSIM = %.2f/%.4f\n', i, ImgNum, psnrs(i, num_deploy), ssims(i, num_deploy));
    
    if nout == 3
        ImgList(i, :) = {y0, yhat};
    end
end
toc

%%
switch nout
    case 1
        varargout = {psnrs};
    case 2
        varargout = {psnrs, ssims};
    case 3
        varargout = {psnrs, ssims, ImgList};
end

fprintf('\tFor Bicubic, mean(PSNR/SSIM) = %.2f/%.4f\n', mean(psnrs(:, 1)), mean(ssims(:, 1)));
if ~isempty(hDeploy)
    fprintf('\tFor %s, mean(PSNR/SSIM) = %.2f/%.4f\n', netname, mean(psnrs(:, 2)), mean(ssims(:, 2)));
end

